<!DOCTYPE html >
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="chrome=1"/>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    <title>g-code simulator</title>
    <script src="../libs/require.js"></script>
    <script src="../config.js"></script>
    <script>
        requirejs.config({ baseUrl: '../' });
    </script>

    <link rel="shortcut icon" href="../images/icon_fraise_48.png"/>
    <link rel="stylesheet" href="../css/twoDView.css" type="text/css">
    <link rel="stylesheet" href="../css/threeDView.css" type="text/css">
    <style>

        body, html {
            -webkit-box-sizing: border-box;
            -moz-box-sizing: border-box;
            box-sizing: border-box;
            position: absolute;
            height: 100%;
            width: 100%;
            margin: 0;
        }

        body {
            padding-left: 8px;
            padding-right: 8px;
        }

        h1 {
            margin-top: 0;
        }

        .editBlock {
            position: relative;
            float: left;
            width: 39%;
            height: 400px;
            padding: 1px;
            margin: 0;
        }

        .editBlock pre {
            width: 100%;
            height: 350px;
            margin: 0;
        }

        .viewContainer {
            float: right;
            width: 60%;
        }

        #loader {
            display: inline-block;
            background-size: 100% 100%;
            background-image: url(../images/spinner.svg);
            width: 20px;
            height: 20px;
        }

        .boundsTable {
            border-collapse: collapse;
        }

        .boundsTable td {
            border: dashed gray 1px;
            padding: 3px;
        }

        .ThreeDView {
            border: solid gray 1px;
            background: #000;
            height: 400px;
            position: relative;
        }

        .TwoDView {
            border: solid gray 1px;
            background: #000;
            height: 400px;
        }

        #app {
            position: relative;
        }
    </style>
</head>
<body>


<h1>G-Code Q'n'dirty toolpath simulator</h1>

<p>
    Paste your g-code in the left-hand window or drop a file on the page and see the preview of your tool path on the right.<br>
    The right-hand pane are interactive, drag them to change the point of view.
</p>

<div id="app">

</div>
<script id="demoCode" type="application/gcode">G0 Y10 Z-5
G1 Z-10
G1 Y20
G02 X10 Y30 R10
G1 X30
G2 X40 Y20 R10
G1 Y10
G2 X30 Y0 R10
G1 X10
G2 X0 Y10 Z-15 R10 (yeah spiral !)
G3 X-10 Y20 R-10 (yeah, long arc !)
G3 X0 Y10 I10 (center)
G91 G1 X10 Z10
G3 Y10 R5 Z3 (circle in incremental)
Y10 R5 Z3 (again, testing modal state)
G20 G0 X1 (one inch to the right)
G3 X-1 R1 (radius in inches)
G3 X1 Z0.3 I0.5 J0.5 (I,J in inches)
G21 (back to mm)
G80 X10 (do nothing)
G90
G0 X30 Y30 Z30
G18 (X-Z plane)
G3 Z40 I0 K5
G19 (Y-Z plane)
G3 Z50 J0 K5
G17 (back to X-Y plane)
</script>
<script>
    require(['Ember', 'cnc/ui/graphicView', 'cnc/cam/cam', 'cnc/util', 'cnc/ui/gcodeEditor', 'cnc/gcode/gcodeSimulation', 'templates'],
            function (Ember, GraphicView, cam, util, gcodeEditor, gcodeSimulation) {
                var demoCode = $('#demoCode').text();

                Ember.Handlebars.helper('num', function (value) {
                    return new Handlebars.SafeString(Handlebars.Utils.escapeExpression(util.formatCoord(value)));
                });
                Ember.TEMPLATES['application'] = Ember.TEMPLATES['camApp'];

                window.Simulator = Ember.Application.create({
                    rootElement: '#app'
                });

                Simulator.GcodeEditorComponent = gcodeEditor.GcodeEditorComponent;
                Simulator.GraphicView = GraphicView;

                Simulator.ApplicationController = Ember.ObjectController.extend({
                    init: function () {
                        this._super();
                        var _this = this;
                        $(window).on('dragover', function (event) {
                            event.preventDefault();
                            event.dataTransfer.dropEffect = 'move';
                        });

                        $(window).on('drop', function (evt) {
                            evt.preventDefault();
                            evt.stopPropagation();
                            var files = evt.dataTransfer.files;
                            var file = files[0];
                            var reader = new FileReader();
                            reader.onload = function (e) {
                                _this.set('code', e.target.result);
                                _this.launchSimulation();
                            };
                            reader.readAsText(file);
                        });
                        this.launchSimulation();
                    },
                    actions: {
                        simulate: function () {
                            this.launchSimulation();
                        },
                        loadBigSample: function () {
                            this.set('computing', true);
                            var _this = this;
                            require(['text!samples/aztec_calendar.ngc'], function (text) {
                                _this.set('code', text);
                                _this.launchSimulation();
                            });
                        }
                    },
                    launchSimulation: function () {
                        var _this = this;

                        function handleResult(result) {
                            _this.flushFragmentFile();
                            var errors = [];
                            for (var i = 0; i < result.errors.length; i++) {
                                var error = result.errors[i];
                                errors.push({row: error.lineNo, text: error.message, type: "error"});
                            }
                            _this.set('errors', errors);
                            _this.set('bbox', {min: result.min, max: result.max});
                            _this.set('totalTime', result.totalTime);
                            _this.set('lineSegmentMap', result.lineSegmentMap);
                            _this.set('computing', false);
                            console.timeEnd('simulation');
                        }

                        console.time('simulation');
                        this.set('computing', true);
                        _this.set('lineSegmentMap', []);
                        this.get('simulatedPath').clear();
                        gcodeSimulation.parseInWorker(this.get('code'), new util.Point(0, 0, 0),
                                Ember.run.bind(_this, handleResult),
                                Ember.run.bind(_this, function (fragment) {
                                    _this.get('fragmentFile').pushObject(fragment);
                                    Ember.run.throttle(_this, _this.flushFragmentFile, 500);
                                }));
                    },
                    flushFragmentFile: function () {
                        this.get('simulatedPath').pushObjects(this.get('fragmentFile'));
                        this.get('fragmentFile').clear();
                    },
                    formattedTotalTime: function () {
                        var totalTime = this.get('totalTime');
                        var humanized = util.humanizeDuration(totalTime);
                        return {humanized: humanized, detailed: Math.round(totalTime) + 's'};
                    }.property('totalTime'),
                    currentHighLight: function () {
                        return this.get('lineSegmentMap')[this.get('currentRow')];
                    }.property('currentRow', 'lineSegmentMap').readOnly(),
                    code: demoCode,
                    errors: [],
                    bbox: {},
                    totalTime: 0,
                    lineSegmentMap: [],
                    currentRow: null,
                    simulatedPath: [],
                    computing: false,
                    fragmentFile: [],
                    canSelectLanguage: false,
                    usingGcode: true,
                    decorations: []
                });
            });
</script>
<a href="https://github.com/nraynaud/webgcode/"><img
        style="position: absolute; top: 0; right: 0; border: 0;"
        src="https://camo.githubusercontent.com/a6677b08c955af8400f44c6298f40e7d19cc5b2d/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f677261795f3664366436642e706e67"
        alt="Fork me on GitHub"
        data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png"></a>
</body>
</html>

